package com.twinkle.controller;

import com.twinkle.bean.UserInfo;
import com.twinkle.service.ContactsService;
import com.twinkle.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private ContactsService contactsService;

    @RequestMapping(value = "/sign_in")
    public ModelAndView sign_in(HttpServletRequest httpServletRequest, HttpSession session) {
        System.out.println("\n==== Sign-in");
        String username = httpServletRequest.getParameter("lo-username");
        String password = httpServletRequest.getParameter("lo-password");
        System.out.println("== " + username + "," + password);
        UserInfo userInfo = userInfoService.sign_in(username, password);
        if (userInfo != null) {
            System.out.println("==== Sign-in Success");
            session.setAttribute("listAll", contactsService.selectSContact(userInfo.getUserinfo_id()));
            session.setAttribute("para", userInfo);
            return new ModelAndView("main.jsp");
        } else {
            System.out.println("==== Sign-in Failure");
            return new ModelAndView("index.jsp");
        }
    }

    @RequestMapping(value = "/sign_up")
    public ModelAndView sign_up(HttpServletRequest httpServletRequest) {
        System.out.println("\n==== Sign-up");
        String username = httpServletRequest.getParameter("re-username");
        String password = httpServletRequest.getParameter("re-password");
        System.out.println("== " + username + "," + password);
        boolean isSignUp = userInfoService.sign_up(username, password);
        if (isSignUp) {
            System.out.println("==== Sign-up Success");
        } else {
            System.out.println("==== Sign-up Failure");
        }
        return new ModelAndView("index.jsp");
    }

    @RequestMapping(value = "/sign_out")
    public ModelAndView sign_out(HttpSession session) {
        System.out.println("\n==== Sign-out");
        UserInfo userInfo = (UserInfo) session.getAttribute("para");
        System.out.println("== " + userInfo.getUsername());
        session.removeAttribute("para");
        System.out.println("==== Sign-out Success");
        return new ModelAndView("index.jsp");
    }

    @RequestMapping(value = "/updateUser")
    public ModelAndView updateUserInfo(HttpServletRequest httpServletRequest) {
        System.out.println("\n==== Update User");
        UserInfo userInfo = new UserInfo();
        userInfo.setUserinfo_id(Integer.parseInt(httpServletRequest.getParameter("u_userid")));
        userInfo.setUsername(httpServletRequest.getParameter("u_username"));
        userInfo.setPassword(httpServletRequest.getParameter("u_password"));
        System.out.println("==== " + userInfo.toString());
        boolean isUpdate = userInfoService.updateUserInfo(userInfo);
        if (isUpdate) {
            System.out.println("==== Update Success");
        } else {
            System.out.println("==== Update Failure");
        }
        return new ModelAndView("main.jsp");
    }
}
